package com.loveprogrammer.eternity.factory;

import com.alibaba.fastjson.JSONArray;
import com.loveprogrammer.eternity.action.BaseAction;
import com.loveprogrammer.eternity.enums.QualityEnum;
import com.loveprogrammer.eternity.enums.SkillQualityEnum;
import com.loveprogrammer.eternity.factory.template.HeroTemplate;
import com.loveprogrammer.eternity.model.Hero;
import com.loveprogrammer.eternity.model.Monster;
import com.loveprogrammer.eternity.model.game.HeroWrapper;
import com.loveprogrammer.eternity.model.game.PetWrapper;
import com.loveprogrammer.eternity.model.product.Equipment;
import com.loveprogrammer.eternity.skill.Skill;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Scanner;

/**
 * @version 1.0.0
 * @description: 英雄工厂
 * @author: eric
 * @date: 2022-08-09 19:53
 **/
public class HeroFactory extends BaseAction {

    private static List<HeroTemplate> heroTemplates;

    static {
        try {
            // 读取配置文件，然后加载到weaponTemplates中
            ClassLoader classLoader = EquipmentFactory.class.getClassLoader();
            InputStream inputStream = classLoader.getResourceAsStream("hero.json");

            assert inputStream != null;
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            StringBuilder responseStrBuilder = new StringBuilder();

            String inputStr;
            while ((inputStr = streamReader.readLine()) != null)
                responseStrBuilder.append(inputStr);

            heroTemplates = JSONArray.parseArray(responseStrBuilder.toString(),HeroTemplate.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<HeroTemplate> getALLHeroTemplates(){
        return heroTemplates;
    }

    public static HeroWrapper createHero(HeroTemplate template, String name) throws IOException, ClassNotFoundException {
        Hero hero = new Hero(name,template);

        String[] skills = template.getSkills().split(",");
        String[] acquiredLevel = template.getAcquiredLevel().split(",");
        String[] skillQualities = template.getSkillQuality().split(",");
        for (int i = 0; i < skills.length; i++) {
            String skillId = skills[i];
            String skillQuality = skillQualities[i];
            Skill skill = SkillFactory.getNewSkillById(skillId, SkillQualityEnum.getByCode(skillQuality));
            if(skill != null && Integer.parseInt(acquiredLevel[i]) <= hero.getLevel()) {
                hero.acquireSkill(skill);
            }
        }

//        for (String skillId : skills) {
//            Skill skill = SkillFactory.getNewSkillById(skillId);
//            if(skill != null) {
//                hero.acquireSkill(skill);
//            }
//        }
        // 如果是剑士 默认给一把木剑，第一技能
        int heroId = template.getId();
        Equipment weapon = null;
        int weaponType = hero.getTemplate().getWeaponType();
        switch (heroId){
            case 1:
                weapon = EquipmentFactory.getWeaponById(1, QualityEnum.白色);
                hero.wear(weapon,weaponType);
                break;
            case 2:
                weapon = EquipmentFactory.getWeaponById(2, QualityEnum.白色);
                hero.wear(weapon,weaponType);
                break;
            case 3:
                weapon = EquipmentFactory.getWeaponById(3, QualityEnum.白色);
                hero.wear(weapon,weaponType);
                break;
            case 4:
                weapon = EquipmentFactory.getWeaponById(4, QualityEnum.白色);
                hero.wear(weapon,weaponType);
                break;
            // 圣职没有进攻能力，先给一只宠物打架
            case 5:
                weapon = EquipmentFactory.getWeaponById(5, QualityEnum.白色);
                hero.wear(weapon,weaponType);
                break;
            case 6:
                weapon = EquipmentFactory.getWeaponById(6, QualityEnum.白色);
                hero.wear(weapon,weaponType);
                break;
            case 7:
                weapon = EquipmentFactory.getWeaponById(5, QualityEnum.白色);
                hero.wear(weapon,weaponType);
                break;
            case 8:
                weapon = EquipmentFactory.getWeaponById(2, QualityEnum.白色);
                hero.wear(weapon,weaponType);
                break;
            default:
                break;
        }
        HeroWrapper heroWrapper = new HeroWrapper(hero, template.getName());
        if(heroId == 5){
            Monster monster = MonsterFactory.createMonster(101, 1, true);
            PetWrapper petWrapper = new PetWrapper(true,true,monster);
            heroWrapper.getPets().add(petWrapper);
            heroWrapper.getFightPets().add(monster);
        }
        if(weapon != null) {
            heroWrapper.getWarehouse().getEquipmentList().add(weapon);
        }
        return heroWrapper;
    }

    public static HeroTemplate selectHero() {
        // 请选择英雄
        eventManager.notifyMessage("请选择英雄", 1);
        for (int i = 0; i < heroTemplates.size(); i++) {
            eventManager.notifyMessage((i+1) + ":" + heroTemplates.get(i).toString(), 1);
        }
        Scanner input = new Scanner(System.in);
        int choose = input.nextInt();
        while(choose < 0 || choose > heroTemplates.size()){
            eventManager.notifyMessage("请重新选择", 1);
            choose = input.nextInt();
        }
        return heroTemplates.get(choose-1);
    }
}
